angular.module('app', []).controller('firstController',
//				scope是应用在HTML(视图)和JavaScript(控制器之间的),他是一个对象,有相应的方法和属性
				function($scope) {
					var arrText=$.cookie("texts").split(",");
					var arrPrice=$.cookie("price").split(",");
					var arrNumber=$.cookie("number").split(",");
					$scope.Product = [];
					$scope.Product.length = arrText.length;
					console.log(arrText[0])
					for(var i=0;i<arrText.length;i++){
						$scope.Product[i]={
							id: i+1,
							name: arrText[i],
							quantity: arrNumber[i],
							price:arrPrice[i].replace("￥","")
						}
					}

					$scope.totalPrice = function() {
						var total = 0;
						angular.forEach($scope.Product, function(item) {
							total += item.quantity * item.price;
						});
						return total;
					}

					$scope.totalQuantity = function() {
						var total = 0;
						angular.forEach($scope.Product, function(item) {
							total += parseInt(item.quantity);
						});
						return total;
					}

					$scope.remove = function(index) {
						$scope.Product.splice(index, 1);
					}

					$scope.removeall = function() {
						$.cookie("texts",arrtext,{expires: -1 });	
						$.cookie("price",arrprice,{expires: -1 });	
						$.cookie("number",arrnume,{expires: -1 });	
						var index;
						for(index = $scope.Product.length - 1; index >= 0; index--) {
							$scope.remove(index);
						}
					}

					$scope.reduce = function(index) {
						if($scope.Product[index].quantity != 1) {
							$scope.Product[index].quantity--;
						} else {
							var ans = confirm("是否移除该产品？");
							if(ans) {
								$scope.remove(index);
							} else {
								$scope.Product[index].quantity = 1;
							}
						}
					}

					$scope.add = function(index) {
						$scope.Product[index].quantity++;
					}

					$scope.$watch('Product', function(newValue, oldValue) {
						angular.forEach(newValue, function(item, key) {
							if(item.quantity < 1) {
								var ans = confirm("是否移除该产品？");
								if(ans) {
									$scope.remove(key);
								} else {
									item.quantity = oldValue[key].quantity;
								}
								return;
							}
						});
					}, true);
				});
				
$(function(){
	var sum = 0;
	$("tr td input[type=checkbox]").click(function(){
		if ($("input[type=checkbox]").is(":checked") == true){
			sum += parseInt($(this).closest("td").siblings("td.sbb").html());
			console.log(sum);
			$("#sum").html("选择商品的价格为:"+sum);
		}else{
			sum=0;
			$("#sum").html("选择商品的价格为:"+0);
		}
	})
})